package com.example.springrabbitmq.service;

import com.example.springrabbitmq.messaging.LoginMessage;
import com.example.springrabbitmq.messaging.RegistrationMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @Author pan_jin
 * @Description: $
 * @Date: 2022/8/15 10:21
 * @Version: 1.0.0
 * @Slogan: 梦想还是要有的万一实现了呢
 */
@Component
public class QueryMessageListener {
    final Logger logger = LoggerFactory.getLogger(getClass());

    static final String QUEUE_MAIL="q_mail";
    static final String QUEUE_SMS="q_sms";
    static final String QUEUE_APP="q_app";

    @RabbitListener(queues = QUEUE_MAIL)
    public void onRegistrationMessageFromMailQueue(RegistrationMessage message) throws Exception {
        logger.info("queue {} received registration message: {}", QUEUE_MAIL, message);
    }

    @RabbitListener(queues = QUEUE_SMS)
    public void onRegistrationMessageFromSmsQueue(RegistrationMessage message) throws Exception {
        logger.info("queue {} received registration message: {}", QUEUE_SMS, message);
    }

    @RabbitListener(queues = QUEUE_MAIL)
    public void onLoginMessageFromMailQueue(LoginMessage message) throws Exception {
        logger.info("queue {} received message: {}", QUEUE_MAIL, message);
    }

    @RabbitListener(queues = QUEUE_SMS)
    public void onLoginMessageFromSmsQueue(LoginMessage message) throws Exception {
        logger.info("queue {} received message: {}", QUEUE_SMS, message);
    }

    @RabbitListener(queues = QUEUE_APP)
    public void onLoginMessageFromAppQueue(LoginMessage message) throws Exception {
        logger.info("queue {} received message: {}", QUEUE_APP, message);
    }

    @Autowired
    private RabbitAdmin rabbitAdmin;

    //声明邮件队列
    @Bean
    public Queue emailQueue(){
        Queue queue = new Queue(QUEUE_MAIL, true);
        //显式声明邮件队列
        rabbitAdmin.declareQueue(queue);
        return queue;
    }
    @Bean
    public Queue smQueue(){
        Queue queue = new Queue(QUEUE_SMS, true);
        //显式声明邮件队列
        rabbitAdmin.declareQueue(queue);
        return queue;
    }
    @Bean
    public Queue appQueue(){
        Queue queue = new Queue(QUEUE_APP, true);
        //显式声明邮件队列
        rabbitAdmin.declareQueue(queue);
        return queue;
    }

}
